﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;
using Cashier_DataManager.data;
using System.Collections;
using System.IO;

namespace Cashier_DataManager
{
    public partial class MainForm : Form
    {
        //private ProgressBar progressBar;
        private DataTable sqliteTables;
        private String importDbPath;

        /// <summary>
        /// 定义委托
        /// </summary>
        /// <param name="tableIndex">当前table数</param>
        /// <param name="tableName">当前表名</param>
        private delegate void UpdateUI(int tableIndex, String tableName);

        public MainForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 获取Sqlite库中所有表
        /// </summary>
        /// <param name="dbPath"></param>
        private void SelectSqliteTables(String dbPath)
        {
            try
            {
                SQLiteDAO sqliteDAO = new SQLiteDAO(dbPath);
                DataTable dt = sqliteDAO.GetSQLiteTables();
                if (dt != null && dt.Rows.Count > 0)
                {
                    sqliteTables = dt;
                }
                else
                {
                    MessageBox.Show("数据源不正确！");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 选择SQLite数据库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_selectSqlite_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.AddExtension = false;
            dialog.Filter = "SQLite文件|*.s3db";
            DialogResult result = dialog.ShowDialog();

            if (result == DialogResult.OK)
            {
                textBox_dbPath.Text = dialog.FileName;
                this.SelectSqliteTables(dialog.FileName);
                label_loadSqlite.Text = "加载数据源成功！";
            }
        }

        /// <summary>
        /// 清空自带表数据
        /// </summary>
        private String emptyNewCashierDB()
        {    
            String dbPath = System.Windows.Forms.Application.StartupPath + @"\cashier.s3db";
            SQLiteDAO sqliteDAO = new SQLiteDAO(dbPath);
            DataTable dt = sqliteDAO.GetSQLiteTables();
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    sqliteDAO.EmptyTable(row[0].ToString());
                }
            }
            return dbPath;
        }

        /// <summary>
        /// 清空数据事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_start_Click(object sender, EventArgs e)
        {
            try
            {
                button_start.Enabled = false;
                importDbPath = this.emptyNewCashierDB();
                if (sqliteTables != null && sqliteTables.Rows.Count>0)
                {
                    //progressBar = progressBar1;
                    int tableCount = sqliteTables.Rows.Count;

                    //设置进度条最小值和最大值
                    progressBar1.Minimum = 0;
                    progressBar1.Maximum = tableCount;
                    
                    
                    String tableName = String.Empty;
                    for (int i = 0; i < tableCount; i++ )
                    {
                        tableName = sqliteTables.Rows[i][0].ToString();
                        Console.WriteLine(tableName);
                        BeginInvoke(new UpdateUI(ProcessBarGrow), new object[] { i + 1, tableName });
                        
                    }
                }
                else
                    MessageBox.Show("没有数据！");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #region 刷新进度条

        /// <summary>
        /// 刷新进度条
        /// </summary>
        /// <param name="tableIndex"></param>
        private void ProcessBarGrow(int tableIndex, String tableName)
        {
            SQLiteDAO sqliteDAO = new SQLiteDAO(textBox_dbPath.Text.Trim());
            sqliteDAO.ImportData(importDbPath, tableName);
            label_table.Text = tableName;
            progressBar1.Value = tableIndex;           
            if (progressBar1.Maximum == tableIndex)
            {
                label_table.Text = String.Empty;
                button_start.Enabled = true;

                MessageBox.Show("修复完毕，点击确定导出修复的数据库！");
                SaveFileDialog fileDialog = new SaveFileDialog();
                fileDialog.Filter ="数据文件|*.s3db";
                fileDialog.DefaultExt = ".s3db";
                fileDialog.FileName = "cashier.s3db";
                fileDialog.AddExtension = true;
                fileDialog.OverwritePrompt = true;
                DialogResult result = fileDialog.ShowDialog();
                if (result == DialogResult.OK)
                {
                    String filePath = System.Windows.Forms.Application.StartupPath + @"\cashier.s3db";
                    FileInfo saveFile = new FileInfo(filePath);
                    String selectPath = fileDialog.FileName;
                    FileInfo oldFile = new FileInfo(selectPath);
                    if (oldFile.Exists)
                    {
                        oldFile.Delete();
                    }
                    saveFile.CopyTo(selectPath);
                }
            }
        }

        #endregion
    }
}
